import { patch } from "./vnode/patch"

import watcher from "./observe/watcher.js"

export function mountComponents(vm, el) {
    // 源码
    callHook(vm, 'beforeMounted')

    let updataComponent = () => {
        vm._updata(vm._render()) // (1)
    }
    new watcher(vm, updataComponent, () => {}, true)

    callHook(vm, 'mounted')
}

export function lifecycleMixin(Vue) {
    Vue.prototype._updata = function(vnode) { // vnode => 真实的dom
        let vm = this
        // 两个参数 （1）旧dom (2)vnode
        vm.$el = patch(vm.$el, vnode)
    }
}

// 生命周期调用
export function callHook(vm, hook) {
    const handlers = vm.$options[hook]
    if(handlers) {
        for(let i = 0; i < handlers.length; i++) {
            handlers[i].call(vm)
        }
    }
}